package com.namecard.namecardapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.namecard.namecardapi.entity.CardInfo;
import com.namecard.namecardapi.entity.DTO.TagDTO;
import com.namecard.namecardapi.entity.TagInfo;
import com.namecard.namecardapi.entity.vo.TagVO;
import com.namecard.namecardapi.mapper.CardInfoMapper;
import com.namecard.namecardapi.mapper.TagInfoMapper;
import com.namecard.namecardapi.mapper.UserInfoMapper;
import com.namecard.namecardapi.service.TagInfoService;
import com.namecard.namecardapi.util.TagUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxb
 * @since 2023-07-04
 */
@Service
public class TagInfoServiceImpl extends ServiceImpl<TagInfoMapper, TagInfo> implements TagInfoService {
    @Resource
    private TagInfoMapper tagInfoMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private CardInfoMapper cardInfoMapper;

    @Resource
    private TagUtil tagUtil;

    @Override
    public List<String> createTag(List<TagVO> tagVOList,String userId) {
        //获取标签集合的id数组
        List<String> userIdList = new ArrayList<>();
        // 采用stream的并发流实现链表的遍历
        tagVOList.stream()
                .parallel()
                .filter(Objects::nonNull)
                .map(tagVO -> tagVO.getId().toString())
                .forEach(userIdList::add);
        //userIdList转为jsonString
        String jsonString = JSON.toJSONString(userIdList);
        //更新数据库
        cardInfoMapper.update(null, new UpdateWrapper<CardInfo>()
                .eq("user_id", userId)
                .set("tag_list", jsonString));
        return tagVOList.stream().parallel().map(TagVO::getTitle).collect(Collectors.toList());
    }
    @Override
    public List<TagInfo> getAllTags() {
        // 获取所有标签数据
        return tagInfoMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public List<TagVO> getUserAllTags(String userId) {
        // 获取用户所有标签数据
        String stringTagList = userInfoMapper.byUserIdSelectTagList(userId);
        // 获取标签数据
        return  tagUtil.getTagList(stringTagList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean userDefinedTag(TagDTO tagDTO, String userId) {
        TagInfo tagInfo = new TagInfo();
        BeanUtils.copyProperties(tagDTO,tagInfo);
        int insert = tagInfoMapper.insert(tagInfo);
        CardInfo cardInfo = cardInfoMapper.selectOne(new QueryWrapper<CardInfo>().eq("user_id", userId));
        List<Integer> tagIds = JSON.parseArray(cardInfo.getTagList(), Integer.class);
        return null ;
    }

}
